| Conditions | 1 |
| Paths | 4 |
| Total Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['helper'], function (helper) { |
||
| 2 | var self = this; |
||
| 3 | |||
| 4 | var ctx; |
||
| 5 | var transform; |
||
| 6 | |||
| 7 | var highlight; |
||
| 8 | |||
| 9 | var nonUplinkColor = '#f2e3c6'; |
||
| 10 | var clientColor = '#e6324b'; |
||
| 11 | |||
| 12 | var cableColor = '#50b0f0'; |
||
| 13 | var highlightColor = 'rgba(255, 255, 255, 0.2)'; |
||
| 14 | |||
| 15 | var labelColor = '#fff'; |
||
| 16 | |||
| 17 | var NODE_RADIUS = 15; |
||
| 18 | var LINE_RADIUS = 12; |
||
| 19 | |||
| 20 | function drawDetailNode(d) { |
||
| 21 | if (transform.k > 1) { |
||
| 22 | ctx.beginPath(); |
||
| 23 | helper.positionClients(ctx, d, Math.PI, d.o.node.statistics.clients, 15); |
||
| 24 | ctx.fillStyle = clientColor; |
||
| 25 | ctx.fill(); |
||
| 26 | ctx.beginPath(); |
||
| 27 | var name = d.o.node_id; |
||
| 28 | if (d.o.node && d.o.node.nodeinfo) { |
||
| 29 | name = d.o.node.nodeinfo.hostname; |
||
| 30 | } |
||
| 31 | ctx.textAlign = 'center'; |
||
| 32 | ctx.fillStyle = labelColor; |
||
| 33 | ctx.fillText(name, d.x, d.y + 20); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | function drawHighlightNode(d) { |
||
| 38 | if (highlight && highlight.type === 'node' && d.o.node === highlight.o) { |
||
| 39 | ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI); |
||
| 40 | ctx.fillStyle = highlightColor; |
||
| 41 | ctx.fill(); |
||
| 42 | ctx.beginPath(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | function drawHighlightLink(d, to) { |
||
| 47 | if (highlight && highlight.type === 'link' && d.o === highlight.o) { |
||
| 48 | ctx.lineTo(to[0], to[1]); |
||
| 49 | ctx.strokeStyle = highlightColor; |
||
| 50 | ctx.lineWidth = LINE_RADIUS * 2; |
||
| 51 | ctx.lineCap = 'round'; |
||
| 52 | ctx.stroke(); |
||
| 53 | to = [d.source.x, d.source.y]; |
||
| 54 | } |
||
| 55 | return to; |
||
| 56 | } |
||
| 57 | |||
| 58 | self.drawNode = function drawNode(d) { |
||
| 59 | ctx.beginPath(); |
||
| 60 | |||
| 61 | drawHighlightNode(d); |
||
| 62 | |||
| 63 | ctx.moveTo(d.x + 3, d.y); |
||
| 64 | ctx.arc(d.x, d.y, 6, 0, 2 * Math.PI); |
||
| 65 | ctx.strokeStyle = nonUplinkColor; |
||
| 66 | ctx.lineWidth = 5; |
||
| 67 | ctx.globalAlpha = 1; |
||
| 68 | ctx.stroke(); |
||
| 69 | |||
| 70 | drawDetailNode(d); |
||
| 71 | }; |
||
| 72 | |||
| 73 | self.drawLink = function drawLink(d) { |
||
| 74 | ctx.beginPath(); |
||
| 75 | ctx.moveTo(d.source.x, d.source.y); |
||
| 76 | var to = [d.target.x, d.target.y]; |
||
| 77 | |||
| 78 | to = drawHighlightLink(d, to); |
||
| 79 | |||
| 80 | ctx.lineTo(to[0], to[1]); |
||
| 81 | ctx.strokeStyle = d.o.type === 'Kabel' ? cableColor : d.color; |
||
| 82 | if (d.o.type === 'fastd' || d.o.type === 'L2TP') { |
||
| 83 | ctx.globalAlpha = 0.2; |
||
| 84 | ctx.lineWidth = 1.5; |
||
| 85 | } else { |
||
| 86 | ctx.globalAlpha = 0.8; |
||
| 87 | ctx.lineWidth = 2.5; |
||
| 88 | } |
||
| 89 | ctx.stroke(); |
||
| 90 | }; |
||
| 91 | |||
| 92 | self.setCTX = function setCTX(newValue) { |
||
| 93 | ctx = newValue; |
||
| 94 | }; |
||
| 95 | self.setHighlight = function setHighlight(newValue) { |
||
| 96 | highlight = newValue; |
||
| 97 | }; |
||
| 98 | self.setTransform = function setTransform(newValue) { |
||
| 99 | transform = newValue; |
||
| 100 | }; |
||
| 101 | return self; |
||
| 102 | }); |
||
| 103 |